﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TARLib
{
    public class ComandaLogCollection : IList<ComandaLog>
    {
        private List<ComandaLog> _InternalList;

        public void CaricaDaComandaID(int comandaID)
        {
            _InternalList = new List<ComandaLog>();

            SqlConnection conn = new SqlConnection(Database.GetDBConnectionString());
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "[Cucina].[SP_ComandaLogByComanda_CL]";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ComandaID", System.Data.SqlDbType.Int) { Value = comandaID });

            SqlDataReader dr = null;
            try
            {
                conn.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    _InternalList.Add(new ComandaLog((int)dr["ComandaLogID"], dr["LogText"].ToString()));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                dr.Dispose();
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }

        #region IList<ComandaLog> Members

        public int IndexOf(ComandaLog item)
        {
            return _InternalList.IndexOf(item);
        }

        public void Insert(int index, ComandaLog item)
        {
            _InternalList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _InternalList.RemoveAt(index);
        }

        public ComandaLog this[int index]
        {
            get
            {
                return _InternalList[index];
            }
            set
            {
                _InternalList[index] = value;
            }
        }

        #endregion

        #region ICollection<ComandaLog> Members

        public void Add(ComandaLog item)
        {
            _InternalList.Add(item);
        }

        public void Clear()
        {
            _InternalList.Clear();
        }

        public bool Contains(ComandaLog item)
        {
            return _InternalList.Contains(item);
        }

        public void CopyTo(ComandaLog[] array, int arrayIndex)
        {
            _InternalList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _InternalList.Count; }
        }

        public bool IsReadOnly
        {
            get { return ((IList<ComandaLog>)_InternalList).IsReadOnly; }
        }

        public bool Remove(ComandaLog item)
        {
            return _InternalList.Remove(item);
        }

        #endregion

        #region IEnumerable<ComandaLog> Members

        public IEnumerator<ComandaLog> GetEnumerator()
        {
            return _InternalList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _InternalList.GetEnumerator();
        }

        #endregion

        internal void Salva(int? ComandaID, SqlTransaction trans)
        {
            throw new NotImplementedException();
        }

        internal void SalvaByComandaID(int p, SqlTransaction trans)
        {
            // throw new NotImplementedException();
        }
    }
}
